import 'package:flutter/material.dart';
import 'package:jd_flutter/base/foundation/flutter_extension.dart';
import 'package:jd_flutter/base/foundation/jd_colors.dart';
import 'package:jd_flutter/base/foundation/jd_device.dart';
import 'package:jd_flutter/base/router/navigator_util.dart';
import 'package:jd_flutter/base/widget/ui/jd_button.dart';

/// 自定义dialog的模板
class BaseDialog extends StatelessWidget {
  const BaseDialog(
      {super.key,
      this.title,
      this.okCallBack,
      this.okButtonTitle,
      this.cancelButtonTitle,
      this.cancelCallBack,
      this.hideOkButton = false,
      required this.customChild,
      this.dialogWith});

  final bool hideOkButton;
  final String? title;
  final String? okButtonTitle;
  final String? cancelButtonTitle;
  // 确认按钮点击
  final VoidCallback? okCallBack;
  final VoidCallback? cancelCallBack;

  final Widget customChild;
  final double? dialogWith;

  goBack(BuildContext context) {
    NavigatorUtil.goBack(context);
  }

  @override
  Widget build(BuildContext context) {
    final Widget dialogTitle = Visibility(
      visible: title.isNotEmptyNullAble,
      child: Padding(
        padding: const EdgeInsets.only(top: 25, bottom: 12),
        child: Text(
          title ?? '',
          style: const TextStyle(color: Color(0xFF1F2A36), fontSize: 18),
        ),
      ),
    );

    final Widget bottomButton = Column(
      children: <Widget>[
        Visibility(
          visible: !hideOkButton,
          child: JDButton(
            backgroundColor: JDColors.mainYellow,
            text: okButtonTitle ?? '确定',
            minHeight: 44,
            minWidth: context.width - 64 * 2,
            onPressed: () {
              okCallBack != null ? okCallBack?.call() : goBack(context);
            },
            radius: 22,
          ),
        ),
        JDButton(
          text: cancelButtonTitle ?? '取消',
          backgroundColor: Colors.transparent,
          textStyle: const TextStyle(color: Color(0xFFBBBBBB), fontSize: 13),
          minHeight: 30,
          minWidth: context.width - 64 * 2,
          onPressed: cancelCallBack,
        ),
        10.vGap,
      ],
    );

    final Widget content = Material(
      borderRadius: BorderRadius.circular(16.0),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          dialogTitle,
          Flexible(child: customChild),
          20.vGap,
          bottomButton,
        ],
      ),
    );

    final Widget body = MediaQuery.removeViewInsets(
      removeLeft: true,
      removeTop: true,
      removeRight: true,
      removeBottom: true,
      context: context,
      child: Center(
        child: SizedBox(
          width: dialogWith ?? JDDevice.width - 80,
          child: content,
        ),
      ),
    );

    return Padding(
      padding: MediaQuery.of(context).viewInsets,
      child: body,
    );
  }
}
